/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.openinsula.vulcano.orm.dao.Dao;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * {@link Dao} implementation that uses Spring's {@link HibernateDaoSupport}.
 * 
 * @author rebola
 * @author yanaga
 * @since 1.0
 * @see Dao
 * @see DaoQuery
 * @see Session
 * @see HibernateDaoSupport
 * @see HibernateTemplate
 */
public class HibernateDao extends HibernateDaoSupport implements Dao {

	protected static final String QUERY_ALIAS = "k";

	@SuppressWarnings("unchecked")
	@Override
	public <T> T findById(final Class<T> entityClass, final Serializable id) {
		return (T) getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public <T> T findUnique(final DaoQuery<T> daoQuery) {
		List<T> queryList = find(daoQuery);

		int resultCount = queryList.size();

		if (resultCount > 1) {
			throw new IllegalStateException("Query did not return unique result!");
		}

		return (resultCount == 0 ? null : queryList.get(0));
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> find(final DaoQuery<T> daoQuery) {
		Session session = getSession(false);

		try {
			return new HibernateQuerySupport().executeQuery(session, daoQuery);
		}
		catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T merge(final T entity) {
		return (T) getHibernateTemplate().merge(entity);
	}

	@Override
	public void persist(final Object... entities) {
		for (Object entity : entities) {
			getHibernateTemplate().saveOrUpdate(entity);
		}
	}

	@Override
	public void remove(final Object... entities) {
		for (Object entity : entities) {
			getHibernateTemplate().delete(entity);
		}
	}

	@Override
	public void refresh(final Object... entities) {
		for (Object entity : entities) {
			getHibernateTemplate().refresh(entity);
		}
	}

	@Override
	public void reattach(final Object... entities) {
		for (Object entity : entities) {
			getHibernateTemplate().lock(entity, LockMode.NONE);
		}
	}

	@Override
	public void evict(final Object... entities) {
		for (Object entity : entities) {
			getHibernateTemplate().evict(entity);
		}
	}

	@Override
	public void clear() {
		getHibernateTemplate().clear();
	}

	@Override
	public void flush() {
		getHibernateTemplate().flush();
	}

}
